import path, { resolve } from 'path';


function getModulePackageName(module: { context: string }) {
    if (!module.context) return null;

    const nodeModulesPath = path.join(__dirname, '../node_modules/');
    if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) {
        return null;
    }

    const moduleRelativePath = module.context.substring(nodeModulesPath.length);
    const [moduleDirName] = moduleRelativePath.split(path.sep);
    let packageName: string | null = moduleDirName;
    // handle tree shaking
    if (packageName && packageName.match('^_')) {
        // eslint-disable-next-line prefer-destructuring
        packageName = packageName.match(/^_(@?[^@]+)/)![1];
    }
    return packageName;
}
export default (config: any) => {
    const jenkinsEnv = process.argv[3];
    console.log('eval-source-map 1', jenkinsEnv)
    if (process.env.NODE_ENV === 'development' || jenkinsEnv === '--sit') {// 开发环境和测试环境打开source-map
        console.log('eval-source-map 2')
        config.merge({
            devtool: 'eval-source-map',
            devServer: {
                hot: false,
                hotOnly: false
            }
        })
    }

    config.optimization
        // share the same chunks across different modules
        .runtimeChunk(false)
        .splitChunks({
            chunks: 'async',
            // name: 'vendors',
            maxInitialRequests: Infinity,
            minSize: 0,
            minChunks: 5, //最小使用到次数，超过2次执行
            cacheGroups: {
                antv: {
                    name: 'antv',
                    chunks: 'all',
                    test: /[\\/]node_modules[\\/](antd)[\\/]/,
                    priority: 11,
                },
                logic: {
                    name: 'logic',
                    chunks: 'all',
                    test: /[\\/]src[\\/](logic)[\\/]/,
                    priority: 11,
                },
                src: {
                    name: 'src',
                    chunks: 'all',
                    test: /[\\/]src[\\/]/,
                    priority: 10,
                },
                handsontable: {
                    name: 'handsontable',
                    chunks: 'all',
                    test: /[\\/]node_modules[\\/](handsontable)[\\/]/,
                    priority: 11,
                },
                vendors: {
                    name: 'vendors',
                    priority: -10,
                    test: /[\\/]node_modules[\\/]/,
                    chunks: 'all',
                    // test: (module: { context: string }) => {
                    //     const packageName = getModulePackageName(module);
                    //     if(packageName=='handsontable'){
                    //         return true;
                    //     }
                    //     // if (packageName) {
                    //     //   return ['bizcharts', '@antv_data-set','monaco-editor'].indexOf(packageName) >= 0;
                    //     // }
                    //     return false;
                    // },
                    // name(module: { context: string }) {
                    //     const packageName = getModulePackageName(module);
                    //     if(packageName=='handsontable'){
                    //         return 'handsontable';
                    //     }
                    //     // if (packageName) { //'handsontable',
                    //     //   if (['bizcharts', '@antv_data-set','monaco-editor'].indexOf(packageName) >= 0) {
                    //     //     return 'monaco';
                    //     //   }
                    //     // }
                    //     return 'misc';
                    // },
                },
            },
        });
    // fix 修复过滤moment情况,过滤其他语言
    config
        .plugin('replace')
        .use(require('webpack').ContextReplacementPlugin)
        .tap(() => {
            return [/moment[/\\]locale$/, /zh-cn/];
        });
    config.module.rule('js').set('test', /\.(js|mjs|jsx|ts|tsx)$/);
    config.module
        .rule('js')
        .include.clear()
        .add(resolve('src'))
        .add(resolve('node_modules/@projectstorm'))
        .end();
    config.module.rule('js').exclude.clear();
};
